<template>
  <div class="myDialog">
    <!-- 弹窗组件 -->
    <el-dialog :visible.sync="dialogFormVisible" :title="title" :before-close="closeDialog" :close-on-click-modal="false" width="650px">
      <el-form
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :disabled="isDisabled"
        label-width="100px"
        class="demo-ruleForm"

      >
        <el-form-item :label="$t('module')" prop="bussinessModule">
          <el-select v-model="form.bussinessModule" filterable>
            <el-option
              v-for="item in moduleList"
              :key="item.name"
              :value="item.name"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('operation')" prop="operation">
          <el-radio-group v-model="form.operation" size="mini" style="width: 220px">
            <el-radio-button :label="1">{{ $t('addTable') }}</el-radio-button>
            <el-radio-button :label="2">{{ $t('addField') }}</el-radio-button>
            <el-radio-button :label="3">{{ $t('updateField') }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('tableName')" prop="tableName">
          <el-input v-model="form.tableName"/>
        </el-form-item>
        <el-form-item :label="$t('fieldName')" prop="fieldName">
          <el-input v-model="form.fieldName"/>
        </el-form-item>
        <el-form-item :label="$t('fieldType')" prop="fieldType">
          <el-input v-model="form.fieldType"/>
        </el-form-item>
        <el-form-item :label="$t('isNull')" prop="isNull">
          <el-radio-group v-model="form.isNull" size="mini" >
            <el-radio-button :label="1">{{ $t('yes') }}</el-radio-button>
            <el-radio-button :label="0">{{ $t('no') }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('defaultValue')" prop="defaultValue">
          <el-input v-model="form.defaultValue"/>
        </el-form-item>
        <el-form-item :label="$t('fieldComment')" prop="comment">
          <el-input v-model="form.comment"/>
        </el-form-item>
        <el-form-item :label="$t('version')" prop="version">
          <el-input v-model="form.version"/>
        </el-form-item>
      </el-form>
      <div v-if="!isDisabled" slot="footer" class="dialog-footer">
        <el-button icon="el-icon-circle-close" @click="closeDialog">{{
          $t("cancel")
        }}</el-button>
        <el-button v-btnThrottle="doSave" type="primary" icon="el-icon-receiving">{{ this.$t('save') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import crudJob from '@/api/system/database'
import enumJob from '@/api/system/enum'
import mySetting from '@/mixins/mySetting'
export default {
  name: 'MyDialog',
  mixins: [mySetting],
  props: {
    form: {
      type: Object,
      default: () => ({})
    },
    isDisabled: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      moduleList: [],
      dialogFormVisible: false,
      rules: {
        bussinessModule: [
          { required: true, message: this.$t('moduleIsRequired'), trigger: 'change' }
        ],
        operation: [
          { required: true, message: this.$t('operationIsRequired'), trigger: 'change' }
        ],
        tableName: [
          { required: true, message: this.$t('tableNameIsRequired'), trigger: 'blur' }
        ],
        fieldName: [
          { required: true, message: this.$t('fieldNameIsRequired'), trigger: 'blur' }
        ]
      } // 表单校验规则
    }
  },
  created() {
    this.getModuleList()
  },
  methods: {
    // 新增数据
    doSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.form.id) {
          // 存在id则是更新
          crudJob.edit({ id: this.form.id, ...this.form }).then(res => {
            this.doMerge(res)
          })
        } else {
          // 不存在id则是新增
          crudJob.add(this.form).then(res => {
            this.doMerge(res)
          })
        }
      })
    },
    getModuleList() {
      enumJob.getList({ enumName: 'BussinessModule' }).then(res => {
        this.moduleList = res.data
      })
    },
    // 打开弹窗
    openDialog(val) {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    },
    // 关闭弹窗
    closeDialog() {
      this.$emit('closeDialog')
      this.dialogFormVisible = false
    }
  }
}
</script>
<style lang="scss" scoped>
.el-form{
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  flex-wrap: wrap;
  .el-form-item{
    width: 50%;
  }
}
</style>
